#ifndef MACHINE_HPP
#define MACHINE_HPP

#include "general.h"
#include "cJSON.h"
#include "Function.hpp"

using namespace std;

class Machine{

public:
	//打开数据库，没有则创建
	 void Sqlite3_open(){
		ret = sqlite3_open("sql.db", &db);
		if(ret != SQLITE_OK){
			cout << "sqlite3_open err" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}

		char *machine_table = "create table if not exists Machine_table(MachineId char primary key, Password char, Master char)";
		ret = sqlite3_exec(db, machine_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);

			return;
		}
			
		char *commodity_table = "create table if not exists Commodity_table(MachineId char, GoodsId char, Name char, Price double, Stock int, Image char, Type char)";
		ret = sqlite3_exec(db, commodity_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);

			return;
		}
	
		char *Sales_records_table = "create table if not exists Sales_records_table(MachineId char, GoodsId char, Price double, Quantity int, Sales double, Time char)";
		ret = sqlite3_exec(db, Sales_records_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);

			return;
		}
	
		return;
	}

	//请求显示广告
	//请求显示商品信息
	//提交订单
	//返回销售记录
	//请求登录进入管理员模式
	//返回操作日志

private:
	sqlite3 *db = NULL;
	char buff[MAXLINE];
	char **result = NULL;
	int row = -1;
	int column = -1;
	int ret;
	char *errmsg;
	char *json_ptr = NULL;
	cJSON *json_ret = cJSON_CreateObject();  //第一层键值
	cJSON *acc;           //帐号
	cJSON *pas;           //密码
	cJSON *que;           //密保问题
	cJSON *ans;           //密保答案
	cJSON *machine_id;    //柜机号
	cJSON *machine_pas;   //柜机密码
	cJSON *GoodsId;       //商品ID
	cJSON *Name;          //商品名
	cJSON *Price;         //商品价格
	cJSON *Stock;         //商品库存
	cJSON *Image;         //商品图片
	cJSON *Type;          //商品类型
};


#endif
